# -*- coding: utf-8 -*-
# Gotacı, gnome için adsl kota gösterme aracıdır.
 # Copyright (C), 2009 Oğuzhan Eroğlu <oguzhan@oguzhaneroglu.com>
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.

 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.

import gtk
import sys
import os

from ConfigParser import RawConfigParser, NoSectionError, NoOptionError

configpath = "%s/.gotaci.cfg" % os.environ["HOME"]

config = RawConfigParser()
try:
	config.readfp(open(configpath))
except IOError:
	config.add_section("user")
	config.write(open(configpath, "wb"))

class settingsWindow(object):
	def __init__(self):
		self.window = gtk.Window()
		self.window.set_title("Gotacı Yapılandırması")
		self.window.resize(330, 210)
		self.window.set_position(gtk.WIN_POS_CENTER)

		# current username and password
		self.cusername, self.cpassword = "", ""
		try:
			self.cusername = config.get("user", "name")
			self.cpassword = config.get("user", "password")
		except NoOptionError:
			config.set("user", "name", "")
			config.set("user", "password", "")

		self.cont = gtk.Fixed()
		self.window.add(self.cont)
		self.cont.show()

		label = gtk.Label("Kullanıcı adı:")
		self.cont.put(label, 35, 40)
		label.show()

		label = gtk.Label("Şifre:")
		self.cont.put(label, 35, 80)
		label.show()

		self.username = gtk.Entry()
		self.username.set_text(self.cusername)
		self.cont.put(self.username, 130, 37)
		self.username.show()

		self.password = gtk.Entry()
		self.password.set_text(self.cpassword)
		self.cont.put(self.password, 130, 77)
		self.password.show()

		self.okButton = gtk.Button("Tamam")
		self.cont.put(self.okButton, 180, 125)
		self.okButton.show()

		self.cancelButton = gtk.Button("İptal")
		self.cont.put(self.cancelButton, 250, 125)
		self.cancelButton.show()

		self.okButton.connect("clicked", self.ok)
		self.cancelButton.connect("clicked", self.cancel)

	def show(self):
		self.window.show()

	def cancel(self, event):
		self.window.hide()

	def ok(self, event):
		try:
			config.set("user", "name", str(self.username.get_text()))
			config.set("user", "password", str(self.password.get_text()))
			config.write(open(configpath, "wb"))
		except NoSectionError:
			config.add_section("user")
			config.set("user", "name", str(self.username.get_text()))
			config.set("user", "password", str(self.password.get_text()))
			config.write(open(configpath, "wb"))
		self.window.hide()